/*
@title: Tic-Tac-Toe
@description: A simple verision of the game tic-tac-toe! Play with a friend and try to place three of your shape in a row before your opponent does.
@author: Atharv Gupta
@tags: ['retro']
@addedOn: 2023-01-19
*/


const player = "p";
const box = "b";
const goal = "g";
const wall = "w";

const x = "X";
const o = "O";
const cursor = "c";

const board0 = "0";
const board1 = "1";
const board2 = "2";
const board3 = "3";
const board4 = "4";
const board5 = "5";
const board6 = "6";
const board7 = "7";
const board8 = "8";

setLegend(
  [ cursor, bitmap`
................
.66666666666666.
.66666666666666.
.66..........66.
.66..........66.
.66..........66.
.66..........66.
.66..........66.
.66..........66.
.66..........66.
.66..........66.
.66..........66.
.66..........66.
.66666666666666.
.66666666666666.
................`],
  [ x, bitmap`
................
................
..00........00..
..000......000..
...000....000...
....000..000....
.....000000.....
......0000......
......0000......
.....000000.....
....000..000....
...000....000...
..000......000..
..00........00..
................
................`],
  [ o, bitmap`
................
................
.....000000.....
...0000000000...
...000....000...
..000......000..
..00........00..
..00........00..
..00........00..
..00........00..
..000......000..
...000....000...
...0000000000...
.....000000.....
................
................`],
  [ board0, bitmap`
...............5
...............5
...............5
...............5
...............5
...............5
...............5
...............5
...............5
...............5
...............5
...............5
...............5
...............5
...............5
5555555555555555`],
  [ board1, bitmap`
5..............5
5..............5
5..............5
5..............5
5..............5
5..............5
5..............5
5..............5
5..............5
5..............5
5..............5
5..............5
5..............5
5..............5
5..............5
5555555555555555`],
  [ board2, bitmap`
5...............
5...............
5...............
5...............
5...............
5...............
5...............
5...............
5...............
5...............
5...............
5...............
5...............
5...............
5...............
5555555555555555`],
  [ board3, bitmap`
5555555555555555
...............5
...............5
...............5
...............5
...............5
...............5
...............5
...............5
...............5
...............5
...............5
...............5
...............5
...............5
5555555555555555`],
  [ board4, bitmap`
5555555555555555
5..............5
5..............5
5..............5
5..............5
5..............5
5..............5
5..............5
5..............5
5..............5
5..............5
5..............5
5..............5
5..............5
5..............5
5555555555555555`],
  [ board5, bitmap`
5555555555555555
5...............
5...............
5...............
5...............
5...............
5...............
5...............
5...............
5...............
5...............
5...............
5...............
5...............
5...............
5555555555555555`],
  [ board6, bitmap`
5555555555555555
...............5
...............5
...............5
...............5
...............5
...............5
...............5
...............5
...............5
...............5
...............5
...............5
...............5
...............5
...............5`],
  [ board7, bitmap`
5555555555555555
5..............5
5..............5
5..............5
5..............5
5..............5
5..............5
5..............5
5..............5
5..............5
5..............5
5..............5
5..............5
5..............5
5..............5
5..............5`],
  [ board8, bitmap`
5555555555555555
5...............
5...............
5...............
5...............
5...............
5...............
5...............
5...............
5...............
5...............
5...............
5...............
5...............
5...............
5...............`],
);

setMap(map`
012
345
678`);
addSprite(2, 2, cursor);

let currSprite = "X";
let boardState = [
  [" ", " ", " "],
  [" ", " ", " "],
  [" ", " ", " "]
];

function checkWin() {
  // vertical
  for(let y = 0; y < 3; ++y) {
    if(boardState[y][0] != " " && boardState[y][0] == boardState[y][1] && boardState[y][1] == boardState[y][2]) {
      return true;
    }
  }
  // horizontal
  for(let x = 0; x < 3; ++x) {
    if(boardState[0][x] != " " && boardState[0][x] == boardState[1][x] && boardState[1][x] == boardState[2][x]) {
      return true;
    }
  }
  // diagonal
  if(boardState[0][0] != " " && boardState[0][0] == boardState[1][1] && boardState[1][1] == boardState[2][2]) {
    return true;
  }
  // antidiagonal
  if(boardState[0][2] != " " && boardState[0][2] == boardState[1][1] && boardState[1][1] == boardState[2][0]) {
    return true;
  }
  return false;
}

function checkTie() {
  for(let y = 0; y < 3; ++y) {
    for(let x = 0; x < 3; ++x) {
      if(boardState[y][x] == " ") {
        return false;
      }
    }
  }
  return true;
}

function endGame(phrase) {
  setMap(map`
.`);
  addText(phrase, {y:7});
}

onInput("w", () => {
  getFirst(cursor).y -= 1;
});

onInput("a", () => {
  getFirst(cursor).x -= 1;
});

onInput("s", () => {
  getFirst(cursor).y += 1;
});

onInput("d", () => {
  getFirst(cursor).x += 1;
});

onInput("i", () => {
  addSprite(getFirst(cursor).x, getFirst(cursor).y, currSprite);
  boardState[getFirst(cursor).y][getFirst(cursor).x] = currSprite;
  if(checkWin()) {
    endGame(currSprite + " wins!");
  }
  if(checkTie()) {
    endGame("It was a tie...");
  }
  if(currSprite == "X") {
    currSprite = "O";
  }
  else {
    currSprite = "X";
  }
});

afterInput(() => {
});
